using Todos.Application.Roles.Commands;
using Todos.Application.Users.Commands;


namespace Todos.Api.Controllers;
[Route("api/integration-events")]
public class IdentityApiIntegrationEventsController(IMediator mediator) : ApiControllerBase
{
    private const string PubsubName = "pubsub";

    [HttpPost("user-sync")]
    [Topic(PubsubName, "UserCreated")]
    [Topic(PubsubName, "UserUpdated")]
    [Topic(PubsubName, "UserSync")]
    public async Task<ActionResult> HandleSyncUserIntegrationEvent([FromBody] SyncUserCommand request)
    {
        var result = await mediator.Send(request);
        if (!result.IsSuccess())
        {
            return BadRequest(result.Message);
        }
        return Accepted();
    }

    [HttpPost("user-delete")]
    [Topic(PubsubName, "UserDeleted")]
    public async Task<ActionResult> HandleDeleteUserIntegrationEvent([FromBody] DeleteUserCommand request)
    {
        var result = await mediator.Send(request);
        if (!result.IsSuccess())
        {
            return BadRequest(result.Message);
        }
        return Accepted();
    }

    [HttpPost("role-sync")]
    [Topic(PubsubName, "RoleCreated")]
    [Topic(PubsubName, "RoleUpdated")]
    [Topic(PubsubName, "RoleSync")]
    public async Task<ActionResult> HandleSyncRoleIntegrationEvent([FromBody] SyncRoleCommand request)
    {
        var result = await mediator.Send(request);
        if (!result.IsSuccess())
        {
            return BadRequest(result.Message);
        }
        return Accepted();
    }

    [HttpPost("role-delete")]
    [Topic(PubsubName, "RoleDeleted")]
    public async Task<ActionResult> HandleDeleteRoleIntegrationEvent([FromBody] DeleteRoleCommand request)
    {
        var result = await mediator.Send(request);
        if (!result.IsSuccess())
        {
            return BadRequest(result.Message);
        }
        return Accepted();
    }
}

